package pe.gob.osinergmin.interfase_siaf.dac.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import pe.gob.osinergmin.interfase_siaf.dac.dao.MaestroDocumentoDao;
import pe.gob.osinergmin.interfase_siaf.dac.domain.MaestroDocumento;

@Repository("maestroDocumentoDao")
public class MaestroDocumentoDaoImpl implements MaestroDocumentoDao {
	
	    private final Log logger = LogFactory.getLog(getClass());
		
		
		private NamedParameterJdbcTemplate jdbcParameterTemplateData;	
		private JdbcTemplate jdbcTemplateData;	
		
		
		
		@Autowired
		public void SetDataSource(DataSource dataSourceData){
			this.jdbcTemplateData = new JdbcTemplate(dataSourceData);
			this.jdbcParameterTemplateData = new NamedParameterJdbcTemplate(dataSourceData);
			
		}

		/**DECLARACION DE SQL**/
		private static final String SQL_SELECT_MADOC ="SELECT cod_doc, nombre FROM maestro_documento ";
		private static final String SQL_SELECT_MADOC_PARAMETER ="SELECT cod_doc, nombre FROM maestro_documento WHERE cod_doc =:codDoc";
		private static final String SQL_SELECT_MADOC_PARAMETER1 ="SELECT cod_doc, nombre FROM maestro_documento WHERE nombre =:nombre";
        
		private static final String SQL_SELECT_MADOC_COD_DOC ="SELECT COUNT(cod_doc) FROM maestro_documento WHERE cod_doc =:codDoc";
		
		
		
		
		@Override
		public List<MaestroDocumento> listarMaestroDocumento() {
			logger.info("Ingresando a listar maestro documento"); 
			RowMapper<MaestroDocumento> lista = ParameterizedBeanPropertyRowMapper.newInstance(MaestroDocumento.class);
			return this.jdbcTemplateData.query(SQL_SELECT_MADOC, lista);
		}



		@Override
		public List<MaestroDocumento> listarMaestroDocumentoParameter(
				MaestroDocumento maestroDoc) {
			logger.info("Ingresando a listar maestro documento parameters"); 
			SqlParameterSource parameters = new BeanPropertySqlParameterSource(maestroDoc);
			RowMapper<MaestroDocumento> lista = ParameterizedBeanPropertyRowMapper.newInstance(MaestroDocumento.class);
			if(StringUtils.isNoneBlank(maestroDoc.getCodDoc())){ 
				return this.jdbcParameterTemplateData.query(SQL_SELECT_MADOC_PARAMETER, parameters, lista);	
			}else if(StringUtils.isNoneBlank(maestroDoc.getNombre())){
				return this.jdbcParameterTemplateData.query(SQL_SELECT_MADOC_PARAMETER1, parameters, lista);	
			}else{
				return null;
			}			
		}	
	
		@Override
		public int verificarCodDoc(MaestroDocumento m) throws Exception {		 
			SqlParameterSource parameters = new BeanPropertySqlParameterSource(m);			
		    return this.jdbcParameterTemplateData.queryForInt(SQL_SELECT_MADOC_COD_DOC, parameters);	
		}
		     

}
